<template>
    <el-container>
        <!-- 头部区域 -->
        <el-header>
            <div class="header_left">
                <img src="../assets/logoo.png" alt="">
                <div class="title">电商后台管理系统</div>
            </div>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse?'64px':'200px'">
                <div class="top_bar" @click="toggleCollapse">|||</div>

                <el-menu
                    :default-active="$route.path"
                    class="el-menu-vertical-demo"
                    router
                    :collapse="isCollapse"
                    background-color="#333744"
                    text-color="#fff"
                    active-text-color="#ffd04b"
                    >
                    <!-- 一级菜单 -->
                    <el-submenu :index="item.id+''" v-for="item in asideList" :key="item.id">
                        <template slot="title">
                            <i :class="iconsObj[item.id]" style="margin-right:10px;"></i> 
                            <span slot="title">{{item.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/'+it.path" v-for="(it,ind) in item.children" :key="ind">
                            <i class="el-icon-menu"></i>
                            <span slot="title">{{it.authName}}</span>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 主体结构 -->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      asideList: [],
      isCollapse: false,
      iconsObj: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      }
    };
  }, // 组件方法

  methods: {
    //侧边栏收缩
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    //侧边栏数据渲染
    async getAside() {
      let res = await this.$http.get("/menus");
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg);
      // console.log("aside", res);
      this.asideList = res.data;
    },

    //点击退出登录
    logout() {
      //  1：判断是否退出
      this.$confirm("您确定要退出吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          //确定
          // console.log(1)
          // 清空token
          window.sessionStorage.clear();
          // 跳转到登录页
          this.$router.push("/login");
        })
        .catch(() => {
          //取消
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    }
  },

  mounted() {
    this.getAside();
  }
};
</script> 

<style  lang='scss' scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  color: #333;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .header_left {
    display: flex;
    align-items: center;
    .title {
      color: white;
      margin-left: 20px;
      font-size: 25px;
    }
  }
  .el-button {
    height: 40px;
    line-height: 15px;
  }
}

.el-aside {
  background-color: #333744;
  color: #333;
  .el-menu{
    border-right: none;
  }
  .top_bar {
    text-align: center;
    color: #fff;
    background-color: #4a5064;
    font-size: 12px;
    height: 22px;
    cursor: pointer;
    letter-spacing: 2px;
  }
}

.el-main {
  background-color: #eaedf1;
  color: #333;
}
</style>